<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DingdController extends Controller
{
   
    public function postList(Request $request)
    {    	
        // dd($request->all());
        // 用户id    
       $uid = $request['id'];
       // dd($uid);
       // 书本id
       $gid = $request['gid'];
        // 书本数量
       $inum = $request['inum'];
       // dd($inum);

       $res = DB::table('goods')->where('id',$gid)->get();
       // dd($res);
    
       foreach($res as $k=>$v)
       {
        // 商品单价
        $a = $v->price;
        // 商品名称
        $b = $v->name;
       }
       
       // dd($b);

        return view('home.dingdan.order',['uid'=>$uid,'gid'=>$gid,'price'=>$a,'num'=>$inum,'gname'=>$b]);

    }

    // 插入数据
    public function postInsert(Request $request)
    {
        // dd($request->all());

        // 用户id
         $uid = $request->all();
        // dd($uid);
         $id = $uid['uid'];
         // dd($id);
         $payment = $uid['payment'];
         // dd($payment);

        // 判断用户是否登录
        if(!$id)
        {
            return back('home/dingdan/list')->with('请先登录用户');
        }

        $pay = $request->input('payment');
        // dd($pay);
        if($pay == 'hd')
        {
            $a = '货到付款';
        }else{
            $a = '在线支付';
        }
    	// 判断付款方式payment
    	$a = $request->input('payment');
    	if($a == 'zx')
    	{
    		$a = '在线支付';
    	}else{
    		$a = '货到付款';
    	}
    // dd($a);
    // 判断邮寄方式send
    	$b = $request->input('send');
    	if($b == 'st')
    	{
    		$b = '申通快递';
    	}else if($b == 'yt'){
    		$b = '圆通快递';
    	}else{
    		$b = '京东快递';
    	}
    // dd($b);
    	$itime = time();
    	$time = $itime + 86400;
    	$time1 = date('Y-m-d H:m:s',$itime);
    	$time2 = date('Y-m-d H:m:s',$time);
    // dd($time2);
    // 判断收货人电话

        $this->validate($request, [
            'phone' => 'required|regex:[1\d{10}]',
        ],[
            'phone.required'=>'电话不能为空',
            'phone.regex'=>'电话格式不正确',
        ]);
        // dd($request->input('sheng','shi'));

    // 地址判断
    	if(empty($request->input('sheng'))){
    		return back()->with('error','未选择地址');
    	}
    // 查询的地址
        $da = $request->only(['0','1','2']);

    // $data = $request->only(['sheng','shi','xian']);

    	$sheng = $request->input('sheng');
    	$shi = $request->input('shi');
    	$xian = $request->input('xian');
    	$dz = $request->input('dz');
    // 拼接路径
        $sheng = $da['0'];
        $shi = $da['1'];
        $xian = $da['2'];
    // dd($data);
    	$address = $sheng.$shi.$xian.$dz;
    // dd($address);
    	$dt['uid'] = $request->input('uid');
    	$dt['address'] = $address;
    	$dt['itime'] = $time1;
    	$dt['stime'] = $time2;

    	$dt['send'] = $b;

    	$dt['payment'] = $a;

        $dt['gname'] = $request->input('gname');
        $dt['phone'] = $request->input('phone');
    // dd($dt);
    	$data = DB::table('order')->insertGetId($dt); 
        // 获取订单id->oid来查询购物
            $s =['oid'=>$data];
            // dd($s);
        //账单
        $tt = $request->only(['nid','gid','inum','price','figure','payment','oid']);
        $tt['payment'] = $a;
        $tt['oid'] = $s['oid'];
        $tt['figure'] = $payment;
        $tt['nid'] = $id;
        // dd($tt);
        // dd($request->input('gname'));
        
        // dd($tt['gname']);
        $res = DB::table('otable')->insert($tt);

        // dd($res);
    	// 判断
    	if($data && $res)
   		{
   			return redirect('home/grdd/list/'.$s['oid'])->with('success','订单提交成功');
   		}else{
   			return back()->with('error','订单提交失败');
   		}
    }


    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $as = $request['figure'];
        $oid = $request['oid'];

        // dd($as);
        if($as)
        {
            $a = '已付款';
        }else{
            $a = '未付款';
        }


        $ad['ly'] = $request['ly'];
        $ad['gds'] = $request['gds'];
        // dd($ad);
        // dd($a);
        $res = DB::table('otable')->where('oid',$oid)->update(['zt'=>$a]);
        // dd($res);
        $data = DB::table('ly')->insert($ad);
        // dd($data);

        if($data)
        {
            return redirect('home/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
}